﻿using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace twilters
{
    public class FilterWindowModel : INotifyPropertyChanged
    {
        private FilterSheet currentFilterSheet;

        public event PropertyChangedEventHandler PropertyChanged;

        public FilterWindowModel()
        {
            FindOrCreateCurrentFilterSheet();
        }

        public FilterSheet CurrentFilterSheet
        {
            get { return currentFilterSheet; }
        }

        public ObservableCollection<TweetField> FieldOptions
        {
            get
            {
                ObservableCollection<TweetField> list = new ObservableCollection<TweetField>();
                list.Add(TweetField.ScreenName);
                list.Add(TweetField.Text);
                return list;
            }
        }

        public ObservableCollection<MatchType> MatchOptions
        {
            get
            {
                ObservableCollection<MatchType> list = new ObservableCollection<MatchType>();
                list.Add(MatchType.Exact);
                list.Add(MatchType.Contains);
                return list;
            }
        }

        public void RemoveRowFromCurrentFilterSheet(int index) {
            CurrentFilterSheet.LineItems.RemoveAt(index);
        }

        public void InsertRowIntoCurrentFilterSheet(int index, FilterLineItem lineItem)
        {
            CurrentFilterSheet.LineItems.Insert(index, lineItem);
        }

        public void SaveCurrentFilterSheet() {

        }

        private void NotifyPropertyChanged(string info) {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public void AddNewRow(int index)
        {
            FilterLineItem item = new FilterLineItem();
            //item.SelectedField = TweetField.Text;
            //item.SelectedMatchType = MatchType.Exact;
            InsertRowIntoCurrentFilterSheet(index, new FilterLineItem());
        }

        private void FindOrCreateCurrentFilterSheet() {
            if (currentFilterSheet == null)
            {
                currentFilterSheet = new FilterSheet();
                AddNewRow(0);
            }
            NotifyPropertyChanged("CurrentFilterSheet");
        }
    }
}
